package me.fung.rem.bean.repo

import me.fung.rem.bean.database
import org.ktorm.dsl.eq
import org.ktorm.entity.find
import org.ktorm.ksp.api.PrimaryKey
import org.ktorm.ksp.api.Table
import java.text.SimpleDateFormat
import java.util.*
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

@Table
data class DBConfig(
    @PrimaryKey
    val name: String,
    var value: String,
    var info: String,
    // type: 1 String,2 integer,3 Date YYYY-MM-DD HH:mm:SS ,4 Float 5 Boolean
    var type: Int,
)

fun DBConfig.add(): DBConfig {
    database.dBConfigs.add(this)
    return this
}

fun sdf(): SimpleDateFormat {
    return SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
}

@Suppress("UNCHECKED_CAST")
class DBConfigProxy<T>(
    private val name: String,
    value: String,
    info: String,
    type: Int,
) : ReadWriteProperty<Any?, T> {
    private var config: DBConfig

    init {
        config = database.dBConfigs.find { it.name eq name } ?: DBConfig(
            name = name,
            value = value,
            info = info,
            type = type
        ).add()
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        config = database.dBConfigs.find { it.name eq name } ?: throw IllegalStateException("Config not found: $name")
        return when (config.type) {
            2 -> config.value.toInt() as T
            3 -> sdf().parse(config.value) as T
            4 -> config.value.toFloat() as T
            5 -> config.value.toBoolean() as T
            else -> config.value as T
        }
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        config.value = when (config.type) {
            2 -> value.toString()
            3 -> sdf().format(value as Date)
            4 -> value.toString()
            5 -> value.toString()
            else -> value as String
        }
        database.dBConfigs.update(config)
    }
}

private fun date(): String {
    return sdf().format(Date())
}

val configClientID by DBConfigProxy<String>("client_id", UUID.randomUUID().toString(), "客户端ID", 1)
var configPassword by DBConfigProxy<String>("password", "", "登陆密码", 1)
var configVersion by DBConfigProxy<Float>("database_version", "1.0", "数据库版本", 4)

// 窗口配置
var configWindowPlacement by DBConfigProxy<Int>("window_placement", "1", "窗口:0-Floating,1-Maximized,2-Fullscreen", 2)
var configWindowPositionX by DBConfigProxy<Float>("window_position_x", "400", "窗口位置X", 4)
var configWindowPositionY by DBConfigProxy<Float>("window_position_y", "200", "窗口位置Y", 4)
var configWindowSizeWidth by DBConfigProxy<Float>("window_size_width", "800", "窗口大小宽度", 4)
var configWindowSizeHeight by DBConfigProxy<Float>("window_size_height", "600", "窗口大小高度", 4)

// 自动登录
var configLastLogin by DBConfigProxy<Date>("last_login", date(), "最后登陆时间", 3)
var configCacheInput by DBConfigProxy<String>("cache_input", "", "自动登录时的输入", 1)
var configAutoSignUp by DBConfigProxy<Boolean>("auto_sign_up", "", "是否使用自动登录", 5)
var configAutoLoginDate by DBConfigProxy<Int>("auto_login_date", "3", "自动登陆天数", 2)

// 界面配置
var configUseAddCard by DBConfigProxy<Boolean>("conf_use_add_card", "", "添加按钮使用卡片", 5)

// 功能配置
var configSearchDays by DBConfigProxy<Int>("conf_search_days", "30", "搜索记录天数", 2)